package com.manage.system.svc.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.common.core.result.JsonResult;
import com.manage.common.core.utils.ContextHolderUtils;
import com.manage.common.core.utils.StringUtils;
import com.manage.system.api.constants.ResponseEnum;
import com.manage.system.api.entity.SysDept;
import com.manage.system.api.request.dept.DeptAddOrEditParam;
import com.manage.system.api.request.dept.DeptQueryParam;
import com.manage.system.api.service.ISysDeptService;
import com.manage.system.svc.mapper.SysDeptMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统部门表 服务实现类
 * </p>
 *
 * @author zhangqing
 * @since 2021-03-05
 */
@Service
@AllArgsConstructor
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService {

    private final SysDeptMapper sysDeptMapper;

    @Override
    public JsonResult findDeptList(DeptQueryParam deptQueryParam) {
        LambdaQueryWrapper<SysDept> lambda = new QueryWrapper<SysDept>().lambda()
                .eq(SysDept::getDelFlag,Boolean.FALSE)
                .eq(!StringUtils.isEmpty(deptQueryParam.getDeptName()),SysDept::getDeptName, deptQueryParam.getDeptName())
                .eq((!StringUtils.isNull(deptQueryParam.getStatus())),SysDept::getStatus,deptQueryParam.getStatus());
        List<SysDept> sysDepts = sysDeptMapper.selectList(lambda);
       return JsonResult.success(sysDepts);
    }

    @Override
    public JsonResult findAllDeptTree() {
        List<SysDept> sysDepts = sysDeptMapper.selectList(Wrappers.emptyWrapper());
        // 初始化树形配置类
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig()
                .setIdKey("id")
                .setWeightKey("orderNum")
                .setParentIdKey("parentId");
        //转换器
        List<Tree<String>> build = TreeUtil.build(sysDepts, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId().toString());
                    tree.setParentId(treeNode.getParentId().toString());
                    tree.setWeight(treeNode.getOrderNum());
                    tree.setName(treeNode.getDeptName());
                    tree.putExtra("status", treeNode.getStatus());
                    tree.putExtra("title", treeNode.getDeptName());
                    tree.putExtra("key", treeNode.getId().toString());
                    tree.putExtra("delFlag", treeNode.getDelFlag());
                    tree.putExtra("createStaff", treeNode.getCreateStaff());
                    tree.putExtra("createTime", treeNode.getCreateTime());
                    tree.putExtra("updateStaff", treeNode.getUpdateStaff());
                    tree.putExtra("updateTime", treeNode.getUpdateTime());
                });
        return JsonResult.success(build);
    }

    @Override
    public JsonResult addDept(DeptAddOrEditParam deptAddOrEditParam) {
        // 查询上级部门
        SysDept parentDept = sysDeptMapper.selectById(deptAddOrEditParam.getParentId());
        ResponseEnum.PARENT_DEPT_NOT_EXISTS.assertIsEmpty(parentDept);
        SysDept sysDept = new SysDept();
        BeanUtils.copyProperties(deptAddOrEditParam,sysDept);
        sysDept.setAncestors(String.format("%s,%s",parentDept.getAncestors(),sysDept.getParentId()));
        sysDept.setCreateStaff(ContextHolderUtils.getCurrentUser().getUserId());
        return JsonResult.toJsonResult(sysDeptMapper.insert(sysDept));
    }

    @Override
    public JsonResult delDeptById(Long deptId) {
        SysDept sysDept = sysDeptMapper.selectById(deptId);
        ResponseEnum.DEPT_NOT_EXISTS.assertIsEmpty(sysDept);
        sysDept.setDelFlag(Boolean.TRUE);
        return JsonResult.toJsonResult(sysDeptMapper.updateById(sysDept));
    }

    @Override
    public JsonResult editDept(DeptAddOrEditParam deptAddOrEditParam) {
        LambdaQueryWrapper<SysDept> lambda = new QueryWrapper<SysDept>().lambda()
                .eq(SysDept::getId, deptAddOrEditParam.getId())
                .eq(SysDept::getDelFlag, Boolean.FALSE);
        SysDept sysDept = sysDeptMapper.selectOne(lambda);
        ResponseEnum.DEPT_NOT_EXISTS.assertIsEmpty(sysDept);
        BeanUtils.copyProperties(deptAddOrEditParam,sysDept);
        SysDept parentDept = sysDeptMapper.selectById(deptAddOrEditParam.getParentId());
        ResponseEnum.PARENT_DEPT_NOT_EXISTS.assertIsEmpty(parentDept);
        sysDept.setAncestors(String.format("%s,%s",parentDept.getAncestors(),sysDept.getParentId()));
        sysDept.setUpdateStaff(ContextHolderUtils.getCurrentUser().getUserId());
        return JsonResult.toJsonResult(sysDeptMapper.updateById(sysDept));
    }
}
